import {Component} from '@angular/core';
import {IonicPage, NavController, NavParams} from 'ionic-angular';
import {HttpServicesProvider} from "../../providers/http-services/http-services";
import {UtilsProvider} from "../../providers/utils/utils";
import {ValidateProvider} from "../../providers/validate/validate";

@IonicPage({
    segment: 'service/agency',
    defaultHistory: ['MemberPage', 'ServicePage']
})
@Component({
    selector: 'page-service-agency',
    templateUrl: 'service-agency.html',
})
export class ServiceAgencyPage {

    //BBS餐饮故事ID
    typeId: number = 54;
    banners: any;
    notices: any;
    page: number = 1;
    infolist = [];
    //评论框 默认隐藏
    commentData = {};

    constructor(
        public navCtrl: NavController,
        public navParams: NavParams,
        public validate: ValidateProvider,
        public httpService: HttpServicesProvider,
        public util: UtilsProvider
    ) {
    }

    ionViewCanEnter() {
        return this.validate.checkLogin();
    }

    ionViewDidEnter() {
        this.getData(null);
    }

    getData(infiniteScroll) {
        //Banner and notices
        this.httpService.requestData('/service/get_ads/type_id/' + this.typeId, response => {
            //获取Banner和滚动文字信息
            let data = response['data'];
            this.banners = data['image_list'];
            this.notices = data['text_list'];
        });

        // list data
        this.httpService.requestData('/service/get_info_list/type_id/' + this.typeId + '/page/' + this.page, response => {
            let data = response['data'];            
            this.infolist = this.infolist.concat(data['info_list']['lists']);
            this.page++;
            if (infiniteScroll) {
                infiniteScroll.complete();
                /*请求完成数据更新页面*/
                if (data['info_list']['lists'].length < data['info_list']['page_size']) {   /*没有数据 禁用上拉更新*/
                    infiniteScroll.enable(false);
                }                               
            }
        });
    }

    doInfinite(infiniteScroll) {
        this.getData(infiniteScroll);
    }

    /**
     * 跳转到信息详情
     * */
    goDetail(amoy_id) {
        this.navCtrl.push('ArticleDetailPage', {id: amoy_id});
    }
}
